package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import uteis.Constantes;
import model.ContaEnergia;

public class ContaEnergiaJDBCCadastro implements ContaEnergiaCadastroDAO {

	Connection conexao;

	public ContaEnergiaJDBCCadastro(Connection conexao) {
		this.conexao = conexao;
	}

	public String salvar(ContaEnergia contaenergia) {
		String sqlquery = "insert into CONTA_ENERGIA(CDCLIENTE, NMTITULAR, DOCUMENTO, CODIGOINSTALACAO, CODIGOCLIENTE, BOLATIVO)"
				+ " values(?,?,?,?,?,?);";
		PreparedStatement pstmt;
		try {
			pstmt = this.conexao.prepareStatement(sqlquery);
			pstmt.setString(1, contaenergia.getCdcliente());
			pstmt.setString(2, contaenergia.getNmtitular());
			pstmt.setString(3, contaenergia.getDocumento());
			pstmt.setString(4, contaenergia.getCodigoinstalacao());
			pstmt.setString(5, contaenergia.getCodigocliente());
			pstmt.setString(6, "S");
			pstmt.execute();

			contaenergia.setNmtitular("");
			contaenergia.setDocumento("");
			contaenergia.setCodigoinstalacao("");
			contaenergia.setCodigocliente("");
			contaenergia.setCdcliente("");
			return ("success");
		} catch (Exception e) {
			e.printStackTrace();
			return ("error");
		}
	}

	public boolean localizarBanco(String numcodinstal) {
		String sqlquery = "SELECT CODIGOINSTALACAO FROM CONTA_ENERGIA WHERE (CODIGOINSTALACAO like '"
				+ numcodinstal
				+ "' and CDCLIENTE = "
				+ Constantes.cdempresas
				+ ") ORDER BY CODIGOINSTALACAO ASC;";
		try {
			String valor = "";
			java.sql.Statement stmt = conexao.createStatement();
			ResultSet rs = null;
			rs = stmt.executeQuery(sqlquery);
			while (rs.next()) {
				valor = rs.getString("CODIGOINSTALACAO");
			}
			if (valor.equals("")) {
				return (false);
			} else {
				return (true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return (false);
		}
	}

	public void editar(ContaEnergia contaenergia) {
	}

	public void excluir(ContaEnergia contaenergia) {
	}

	public List<ContaEnergia> ListarContaEnergiaT() {
		return null;
	}

}
